package storeSortingWeighRecord

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/common/utils"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/sorting/internal/svc"
	"insighttracking/apps/sorting/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetStoreSortingWeighRecordApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetStoreSortingWeighRecordApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetStoreSortingWeighRecordApiLogic {
	hostname, _ := os.Hostname()
	return &GetStoreSortingWeighRecordApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetStoreSortingWeighRecordApiLogic) GetStoreSortingWeighRecordApi(req *types.ResetStoreSortingWeighRecordReq) (resp []types.StoreSortingWeighRecordResp, err error) {
	weighRecordEntities, err := service.StoreSortingWeighRecordService.GetStoreSortingWeighRecordApi(l.ctx, req.OrdersID, req.OrdersDetailID)
	if err != nil || len(weighRecordEntities) == 0 {
		return make([]types.StoreSortingWeighRecordResp, 0), nil
	}
	return k.Map(weighRecordEntities, func(item *model.StoreSortingWeighRecordEntity, index int) types.StoreSortingWeighRecordResp {
		return types.StoreSortingWeighRecordResp{
			ID:             item.ID,                                 // 主键id
			EnterpriseID:   item.EnterpriseID,                       // 关联到enterprise表主键id
			OrdersID:       item.OrdersID,                           // 订单表orders表id
			OrdersDetailID: item.OrdersDetailID,                     // 订单详情表orders_details主键id
			Weight:         item.Weight.StringFixed(2),              // 称重重量
			Remark:         item.Remark,                             // 备注
			CreatedAt:      utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:      utils.TimestampToString(item.UpdatedAt), // 更新时间
		}
	}), nil
}
